﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using QXGameEngine.Interface;
using QXGameEngine.Logic;

namespace QXGameEngine.Control {

    public partial class QXMagic : UserControl, IObject {

        /// <summary>
        /// 魔法控件
        /// </summary>
        public QXMagic() {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(100);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();

        }
        /// <summary>
        /// 获取或设置内置计时器
        /// </summary>
        public DispatcherTimer Timer { get; set; }

        /// <summary>
        /// 获取或设置根画布容器
        /// </summary>
        public Canvas RootCanvas {
            get { return this.Container; }
        }

        /// <summary>
        /// 获取父容器
        /// </summary>
        public Canvas ParentCanvas {
            get { return this.Parent as Canvas; }
        }

        /// <summary>
        /// 获取或设置帧推进器
        /// </summary>
        public int FrameCounter { get; set; }

        /// <summary>
        /// 获取或设置魔法当前开始图片列号
        /// </summary>
        public int StartFrame { get; set; }

        /// <summary>
        /// 获取或设置魔法当前结束图片列号
        /// </summary>
        public int EndFrame { get; set; }

        /// <summary>
        /// 获取或设置魔法起效针
        /// </summary>
        public int EffectiveFrame { get; set; }

        /// <summary>
        /// 获取或设置施法者名字
        /// </summary>
        public string MagicOwner { get; set; }

        /// <summary>
        /// 获取或设置魔法代号
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 获取或设置魔法类型
        /// 0:圆形范围群体攻击
        /// 1:激光类直线穿梭群体攻击
        /// </summary>
        public int Mode { get; set; }

        /// <summary>
        /// 获取或设置魔法旋转角度
        /// </summary>
        public double Angle { get; set; }

        /// <summary>
        /// 获取或设置魔法等级
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        /// 获取或设置魔法有效半径
        /// </summary>
        public int Radius { get; set; }

        /// <summary>
        /// 获取或设置魔法的攻击力
        /// </summary>
        public int ATK { get; set; }

        /// <summary>
        /// 获取或设置施放魔法需要消耗的魔力值
        /// </summary>
        public int Consumption { get; set; }

        /// <summary>
        /// 获取或设置魔法附加特效代号
        /// </summary>
        public int AdditionalEffectCode { get; set; }

        /// <summary>
        /// 获取或设置魔法附加特效伤害效果
        /// </summary>
        public double AdditionalEffect { get; set; }

        /// <summary>
        /// 获取或设置魔法附加特效持续时间
        /// </summary>
        public double AdditionalEffectTime { get; set; }

        /// <summary>
        /// 获取或设置魔法效果装饰代号
        /// </summary>
        public int DecorationCode { get; set; }

        /// <summary>
        /// 获取或设置魔法效果装饰帧数(结束帧)
        /// </summary>
        public int DecorationEndFrame { get; set; }

        /// <summary>
        /// 获取或设置装饰特性类型
        /// </summary>
        public DecorationTypes DecorationType { get; set; }

        /// <summary>
        /// 获取或设置装饰宽
        /// </summary>
        public double DecorationWidth { get; set; }

        /// <summary>
        /// 获取或设置装饰高
        /// </summary>
        public double DecorationHeight { get; set; }

        /// <summary>
        /// 获取或设置魔法朝向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 获取或设置控件主体宽
        /// </summary>
        public double BodyWidth {
            get { return Container.Width; }
            set { Container.Width = value; }
        }

        /// <summary>
        /// 获取或设置控件主体高
        /// </summary>
        public double BodyHeight {
            get { return Container.Height; }
            set { Container.Height = value; }
        }
        //精灵状态
        public int status { set; get; }
        /// <summary>
        /// 获取或设置魔法中心点距离左边X坐标
        /// </summary>
        public double CenterX { get; set; }

        /// <summary>
        /// 获取或设置魔法中心点距离顶边Y坐标
        /// </summary>
        public double CenterY { get; set; }

        /// <summary>
        /// 获取或设置精灵坐标(关联属性)
        /// </summary>
        public Point Coordinate {
            get { return (Point)GetValue(CoordinateProperty); }
            set { SetValue(CoordinateProperty, value); }
        }
        public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register(
            "Coordinate",
            typeof(Point),
            typeof(QXMagic),
            new PropertyMetadata(ChangeCoordinateProperty)
        );
        private static void ChangeCoordinateProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXMagic obj = (QXMagic)d;
            if (obj.Visibility == Visibility.Visible) {
                Point p = (Point)e.NewValue;
                obj.SetValue(Canvas.LeftProperty, p.X - obj.CenterX);
                obj.SetValue(Canvas.TopProperty, p.Y - obj.CenterY);
                obj.SetValue(Canvas.ZIndexProperty, (int)p.Y);
            }
        }

        /// <summary>
        /// 计时器事件
        /// </summary>
        public void Timer_Tick(object sender, EventArgs e) {
            
            Container.Background = new ImageBrush() { ImageSource = Super.GetImage(string.Format(@"/Image/Magic/{0}/{1}-{2}.png", Code, (int)Direction, FrameCounter)) };
            if (FrameCounter == EffectiveFrame) {
                Super.DoInjure(this);
            } 
            if (FrameCounter == EndFrame) {
                Timer.Stop();
                Super.RecyclingMagic(this);
                Super.RemoveObject(this, true);
            
            } else {
                FrameCounter += 1;
            }
        }
    }
}
